package com.hzya.frame.web.aop;

import com.hzya.frame.utils.core.entity.BaseEntity;
import com.hzya.frame.utils.help.BeanHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 拦截Action中的save*方法，并绑定公共字段
 * @author Administrator
 *
 */
@Aspect
// @Component
public class ControllerBindAop {
    //@Pointcut("within(@org.springframework.stereotype.Controller *)")
    @Pointcut("execution(* com.hzya.frame..controller.*.save*(..)) || execution(* com.hzya.frame..controller.*.update*(..))")
    public void bindPoint() {
    };
   
    private String execMethods;
    
    @Around("bindPoint()")
    public Object runRound(ProceedingJoinPoint joinPoint) throws Throwable {
	Object result=null;
	Object target= joinPoint.getThis();
	Object[] params=joinPoint.getArgs();
	if(null!=params && 0< params.length){
	    Object param=params[0];
        	if(param instanceof BaseEntity){
        	    try {
        		String [] execMethodsAry= execMethods.split(",");
        		for(int i=0;i<execMethodsAry.length;i++){
        		    BeanHelper.invoke(target, execMethodsAry[i], new Object[]{param});
        		}
        		
        	    } catch (Exception e) {
        		e.printStackTrace();
        	    }finally {
        		result=joinPoint.proceed();
        	    }
        	}else{
        	    result=  joinPoint.proceed();
        	}
	}else{
	    
	    result= joinPoint.proceed();
	}
	return result;
    }

    public String getExecMethods() {
        return execMethods;
    }

    public void setExecMethods(String execMethods) {
        this.execMethods = execMethods;
    }

}
